<?php
// 入口文件
session_start();


// 定义项目根目录
define('ROOT_PATH', dirname(__FILE__));

// 包含配置文件
require_once ROOT_PATH . '/config/config.php';

// 包含数据库类
require_once ROOT_PATH . '/func/db.class.php';

// 包含工具类
require_once ROOT_PATH . '/func/utils.php';

// 包含认证类
require_once ROOT_PATH . '/func/auth.php';

// 获取请求URI
$requestUri = $_SERVER['REQUEST_URI'] ?? '';

// 路由分发
switch ($requestUri) {
    case '/register': 
        require_once ROOT_PATH . '/templates/register.html';
        break;
    case '/login':
        if ($_SERVER['REQUEST_METHOD'] == 'GET') {
            require_once ROOT_PATH . '/templates/login.html';
        } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            require_once ROOT_PATH . '/api/login.php';
        }
        break;
    case '/user':
        if (isAuthenticated()) {
            require_once ROOT_PATH . '/templates/user_center.html';
        } else {
            echo json_encode(['success' => false, 'message' => '未登录']);
        }
        break;
    case '/change_password':
        if (isAuthenticated()) {
            require_once ROOT_PATH . '/templates/change_password.html';
        } else {
            echo json_encode(['success' => false, 'message' => '未登录']);
        }
        break;
    case '/logout':
        session_unset();
        session_destroy();
        header('Location: /login');
        exit;
    default:
        // 默认重定向到登录页
        header('Location: /login');
        exit;
}
